<?php
/**
 * Created by PhpStorm.
 * User: jswei
 * Date: 2018/5/14
 * Time: 13:03
 */
namespace app\admin\controller;

use app\admin\model\AdminLog as model;
use app\admin\validate\Admin as validate;
use think\App;
use think\facade\Hook;

class AdminLog extends Base{
    public function __construct(App $app = null){
        parent::__construct($app);
        $this->validate = new validate;
        $this->model = new model;
    }

    /**
     * 获取列表数据
     * @param array $where
     * @param string $sort
     * @param int $limit
     * @return \think\Response
     * @throws \think\exception\DbException
     */
    public function index($where=[],$sort='',$limit=8){
        $where = $this->_where($where);
        $order = $this->_order($sort);
        if(self::$user['gid']!=-1){
            $where[] = [$this->model->getTable().'.aid','eq',self::$uid];
        }
        $list = $this->model->getList($where,$order,$limit,['guide','password','hash'],true);
        return $this->__s('',$list);
    }

    /**
     * @param int $id
     * @return \think\Response
     */
    public function delete($id=0){
        $info = $this->model->find($id);
        if(!$info){
            return $this->__e("{$this->model->getTableComment()}不存在");
        }
        if(!$info->delete()){
            return $this->__e('删除失败');
        }
        Hook::listen('admin_log',['aid'=>self::$uid,'action'=>"删除【{$this->model->getTableComment()}】，编号：{$id}"]);
        return $this->__s('删除成功');
    }

    /**
     * @param int $id
     * @return \think\Response
     */
    public function read($id=0){
        $info = $this->model->find($id);
        if(!$info){
            return $this->__e("{$this->model->getTableComment()}不存在");
        }
        return $this->__s('',$info);
    }

    public function edit($id){
        $info = $this->model->find($id);
        if(!$info){
            return $this->__e("{$this->model->getTableComment()}不存在");
        }
        return $this->__s('',$info);
    }

    /**
     * @param int $id
     * @return \think\Response
     */
    public function update($id=0){
        $info = $this->model->find($id);
        if(!$info){
            return $this->__e("{$this->model->getTableComment()}不存在");
        }
        $data = $this->request->put();
        if(!$info->save($data)){
            return $this->__e('操作失败');
        }
        Hook::listen('admin_log',['aid'=>self::$uid,'action'=>"更新【{$this->model->getTableComment()}】，编号：{$id}"]);
        return $this->__s('操作成功');
    }
}